package pers.echo.tester.myservice;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.echo.tester.bean.TemplateBean;
import pers.echo.tester.entity.TblBulletins;
import pers.echo.tester.entity.TblCkqinfo;
import pers.echo.tester.entity.TblLandinfo;
import pers.echo.tester.entity.TblTkqinfo;
import pers.echo.tester.enums.ResTypeEnum;
import pers.echo.tester.service.ITblBulletinsService;
import pers.echo.tester.service.ITblCkqinfoService;
import pers.echo.tester.service.ITblLandinfoService;
import pers.echo.tester.service.ITblTkqinfoService;
import pers.echo.tester.utils.SNUtil;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.StringJoiner;

@Service
public class GenTestRes implements IGenTestRes{

    @Autowired
    ITblBulletinsService bulletinsService;

    @Autowired
    ITblCkqinfoService ckqinfoService;

    @Autowired
    ITblTkqinfoService tkqinfoService;


    @Autowired
    ITblLandinfoService landinfoService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void produceCkqList(int number) {

        TblBulletins bulletins = bulletinsService.getblBulletinsTemplate(ResTypeEnum.CKQ);
        // 公告编号  发布状态

        LocalDateTime dt = LocalDateTime.now();
        StringJoiner joiner = new StringJoiner("-");
        joiner
            .add(String.valueOf(dt.getMonth().getValue()))
            .add(String.valueOf(dt.getDayOfMonth()))
            .add(String.valueOf(dt.getHour()))
            .add(String.valueOf(dt.getMinute()))
            .add(String.valueOf(dt.getSecond()));

        bulletins.setFNo(joiner.toString());
        bulletins.setFPublished("Y");

        bulletinsService.save(bulletins);

        TblCkqinfo ckqinfo = ckqinfoService.getCkqinfoTemplate();
        ckqinfo.setBulletinid(bulletins.getBulletinid());
        ckqinfo.setFNo("TEST-CKQ");

        ckqinfoService.save(ckqinfo);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void generateCkqTestSample(TemplateBean templateBean){

        String xzqdm = templateBean.getXzqdm();
        String bulletSn = templateBean.getBulletSn();
        String resSn = templateBean.getResSn();
        String resName = templateBean.getResName();
        String resArea = templateBean.getResArea();
        LocalDateTime startTime = templateBean.getStartTime();
        LocalDateTime bailTime = templateBean.getBailTime();
        LocalDateTime endTime = templateBean.getEndTime();
        BigDecimal creditAmount = templateBean.getCreditAmount();
        BigDecimal startAmount = templateBean.getStartAmount();

        TblBulletins bulletins = bulletinsService.getblBulletinsTemplate(ResTypeEnum.CKQ);
        bulletins.setFXzqdm(xzqdm!=null?xzqdm:"360000");
        bulletins.setFNo(bulletSn!=null?bulletSn: SNUtil.getBulletSn());
        boolean flag = bulletinsService.save(bulletins);

        if(flag){
            TblCkqinfo ckqinfo = ckqinfoService.getCkqinfoTemplate();
            ckqinfo.setBulletinid(bulletins.getBulletinid());
            ckqinfo.setFNo(resSn!=null?resSn: SNUtil.getResSn(ResTypeEnum.CKQ));
            ckqinfo.setFName(resName!=null?resName: "测试矿");
            ckqinfo.setFArea(resArea!=null?resArea: "100");
            ckqinfo.setFBhje(creditAmount!=null?creditAmount: BigDecimal.valueOf(50.0));
            ckqinfo.setFPrice(startAmount!=null?startAmount: BigDecimal.valueOf(100));
            if(startTime!=null){
                ckqinfo.setFBeigintime(startTime);
            }
            if(bailTime!=null){
                ckqinfo.setFBailtime(bailTime);
            }
            if(endTime!=null){
                ckqinfo.setFClosetime(endTime);
                ckqinfo.setFFinishtime(endTime);
            }
            ckqinfoService.save(ckqinfo);
        }


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void generateTkqTestSample(TemplateBean templateBean){

        String xzqdm = templateBean.getXzqdm();
        String bulletSn = templateBean.getBulletSn();
        String resSn = templateBean.getResSn();
        String resName = templateBean.getResName();
        String resArea = templateBean.getResArea();
        LocalDateTime startTime = templateBean.getStartTime();
        LocalDateTime bailTime = templateBean.getBailTime();
        LocalDateTime endTime = templateBean.getEndTime();
        BigDecimal creditAmount = templateBean.getCreditAmount();
        BigDecimal startAmount = templateBean.getStartAmount();

        TblBulletins bulletins = bulletinsService.getblBulletinsTemplate(ResTypeEnum.CKQ);
        bulletins.setFXzqdm(xzqdm!=null?xzqdm:"360000");
        bulletins.setFNo(bulletSn!=null?bulletSn: SNUtil.getBulletSn());
        boolean flag = bulletinsService.save(bulletins);

        if(flag){
            TblTkqinfo tkqinfo = tkqinfoService.getTkqinfoTemplate();
            tkqinfo.setBulletinid(bulletins.getBulletinid());
            tkqinfo.setFNo(resSn!=null?resSn: SNUtil.getResSn(ResTypeEnum.CKQ));
            tkqinfo.setFName(resName!=null?resName: "测试矿");
            tkqinfo.setFArea(resArea!=null?resArea: "100");
            tkqinfo.setFBhje(creditAmount!=null?creditAmount: BigDecimal.valueOf(50.0));
            tkqinfo.setFPrice(startAmount!=null?startAmount: BigDecimal.valueOf(100));
            if(startTime!=null){
                tkqinfo.setFBeigintime(startTime);
            }
            if(bailTime!=null){
                tkqinfo.setFBailtime(bailTime);
            }
            if(endTime!=null){
                tkqinfo.setFClosetime(endTime);
                tkqinfo.setFFinishtime(endTime);
            }
            tkqinfoService.save(tkqinfo);
        }


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeResTestSample() {
        List<TblBulletins> bulletinsList = bulletinsService.list(Wrappers.<TblBulletins>lambdaQuery().likeRight(TblBulletins::getFWord, "测试"));
        for(TblBulletins bulletins: bulletinsList){
            String resType = bulletins.getFType();
            if(ResTypeEnum.LAND.value.equals(resType)){
                landinfoService.remove(Wrappers.<TblLandinfo>lambdaQuery().eq(TblLandinfo::getBulletinid, bulletins.getBulletinid()));
            }else if(ResTypeEnum.CKQ.equals(resType)){
                ckqinfoService.remove(Wrappers.<TblCkqinfo>lambdaQuery().eq(TblCkqinfo::getBulletinid, bulletins.getBulletinid()));
            }else if(ResTypeEnum.TKQ.equals(resType)){
                tkqinfoService.remove(Wrappers.<TblTkqinfo>lambdaQuery().eq(TblTkqinfo::getBulletinid, bulletins.getBulletinid()));
            }
        }
    }
}
